/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.woundedwolf.android.tumbleweed.renderables;

import android.graphics.Bitmap;
import android.opengl.GLUtils;
import com.woundedwolf.android.tumbleweed.elements.Renderable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author FlameDuck
 */
public class DynamicBitmap implements Renderable
{
	private Bitmap bitmap;
	private final Object bitmapLock;

	public DynamicBitmap()
	{
		bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);
		bitmapLock = new Object();
	}

	public void draw(GL10 glContext)
	{
		glContext.glBindTexture(glContext.GL_TEXTURE_2D, 0);
		GLUtils.texImage2D(glContext.GL_TEXTURE_2D, 0, bitmap, 0);

		float[] vertices = new float[8];
		vertices[0] = 0.5f;
		vertices[1] = 0.5f;

		// Allocate a vertexbuffer.
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		FloatBuffer vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		glContext.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		glContext.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);

		glContext.glDrawArrays(GL10.GL_POINTS, 0, 8);

		glContext.glDisableClientState(GL10.GL_VERTEX_ARRAY);

		//TODO: Fix this.

	}

	public int getPixel(int x, int y)
	{
		return bitmap.getPixel(x, y);
	}

	public void setPixel(int x, int y, int color)
	{
		bitmap.setPixel(x, y, color);
	}
}
